import { defineConfig, loadEnv } from 'vite'
import vue from '@vitejs/plugin-vue'
import { resolve } from 'path'
import { createHtmlPlugin } from 'vite-plugin-html'

const getViteEnv = (mode, target) => {
  return loadEnv(mode, process.cwd())[target]
}

// https://vitejs.dev/config/
export default defineConfig(({ command, mode }) => {
  return {
    plugins: [
      vue(),
      createHtmlPlugin({
        inject: {
          data: {
            //将环境变量 VITE_MAP_URL 赋值给 mapBaseUrl 方便 html页面使用 mapBaseUrl 获取地图地址
            mapBaseUrl: getViteEnv(mode, 'VITE_MAP_URL'),
            envMode: getViteEnv(mode, 'VITE_ENV')
          }
        }
      })
    ],
    base: command === 'build' ? '/ivam' : '/',
    resolve: {
      alias: {
        '@': resolve(__dirname, 'src')
      }
    },
    css: {
      preprocessorOptions: {
        scss: {
          additionalData: '@use "sass:math"; @use "./src/style/otherStyle.scss" as *;' // 全局公共样式
        }
      }
    },
    build: {
      outDir: 'ivam' //打包输出文件夹
    },
    server: {
      hmr: true,
      host: '0.0.0.0',
      port: '8080',
      proxy: {
        '/config-center': {
          target: 'http://10.110.104.135/config-center/', // 测试
          changeOrigin: true,
          rewrite: path => path.replace(/^\/config-center/, '/')
        },
        '/micro-smart-application-service/ivam': {
          target: 'http://10.100.35.103:8081/micro-smart-application-service/ivam/', // 测试
          // target: 'http://10.120.8.192:8838/micro-smart-application-service/ivam/', // 本地环境
          // target: 'http://13.148.129.85:8088/micro-smart-application-service/ivam/', // 张家口
          changeOrigin: true,
          rewrite: path => path.replace(/^\/micro-smart-application-service\/ivam/, '/')
        }
      }
    }
  }
})
